using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sedge.Core;
using Sedge.Core.Configuration;
using Sedge.Core.Execution;
using Sedge.Core.Globalization;

namespace Sedge.UI.ViewModel
{
	public class MasterViewModel
	{
		private readonly Suite _suite;
		private readonly List<IStepController> _stepControllers;

		private int _currentView = -1;
		private int _blockedView = -1;

		public MasterViewModel(Suite suite, List<IStepController> stepControllers)
		{
			_suite = suite;
			_stepControllers = stepControllers;
		}

		public string GetCaption()
		{
			return String.IsNullOrEmpty(_suite.Caption) ?
										Locale.Strings.MFormCaption : _suite.Caption;
		}

		public string GetViewCaption()
		{
			return _stepControllers[_currentView].Caption;
		}

		public bool HasNextView()
		{
			return _currentView < _stepControllers.Count - 1;
		}

		public bool HasPrevView()
		{
			return _blockedView < _currentView - 1;
		}

		public Control GetNextView()
		{
			if (!HasNextView())
				return null;

			if (_currentView != -1)
			{
				if (!_stepControllers[_currentView].LeaveStep(true))
					_blockedView = _currentView;
			}

			_currentView++;

			Control view = (Control) _stepControllers[_currentView].View;
			if (!_stepControllers[_currentView].EnterStep(true))
				_blockedView = _currentView;

			return view;
		}

		public Control GetPrevView()
		{
			if (!HasPrevView())
				return null;

			_stepControllers[_currentView].LeaveStep(false);
			_currentView--;

			Control view = (Control)_stepControllers[_currentView].View;
			_stepControllers[_currentView].EnterStep(false);

			return view;
		}

		public void Clean()
		{
            _suite.CleanUp();
		}

		public void Cancel()
		{
			if (Services.ViewManager.IsLocked)
				Services.ViewManager.BreakLock();

			if (_currentView != -1)
				_stepControllers[_currentView].LeaveStep(true);

			Clean();
		}
	}
}